package beanClient;

import beans.Priority;
import beans.Pulje;

import beans.Student;

import beans.Studentsubject;
import beans.Subject;

import domain.*;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.naming.NamingException;

import sesBean.SessionEJB;

public class SessionEJBClient
{
  SessionEJB sessionEJB;
  
  public SessionEJBClient()
  {
    try
    {
      final Context context = getInitialContext();
      sessionEJB =
        (SessionEJB) context.lookup("XPProjekt-SessionEJB#sesBean.SessionEJB");
    }
    catch (NamingException e)
    {

    }
  }

  public List<domain.Subject> getSubjects()  
  {
    List<Subject> subjects = sessionEJB.querySubjectFindAll();
    List<domain.Subject> dSubjects = new ArrayList<domain.Subject>();
    for (int i = 0; i < subjects.size(); i++)
    {
      domain.Subject s = new domain.Subject();
      s.setId(subjects.get(i).getId());
      s.setTitle(subjects.get(i).getTitle());
      dSubjects.add(s);
    }

    return dSubjects;
  }
  
  public boolean saveAllSubjects(List<domain.Subject> subjects)
  {
    boolean retur = true;
    List<Subject> subs = sessionEJB.querySubjectFindAll();
    List<Subject> subs2 = new ArrayList<Subject>();
    List<Boolean> found = new ArrayList<Boolean>();
    try
    {
      for (int i = 0; i < subjects.size(); i++)
      {
        Long id = subjects.get(i).getId();
        Subject sub = new Subject();
        sub.setId(i+0L); //subjects.get(i).getId()
        sub.setTitle(subjects.get(i).getTitle());
        sub.setDescription(subjects.get(i).getBeskrivelse());
        subs2.add(sub);
      }
      for (int i = 0; i < subs.size(); i++)
      {
        found.add(false);
        int j = 0;
        while (!found.get(i)&&j < subs2.size())
        {
          if(subs2.get(j).getId().equals(subs.get(i).getId()))
          {
            found.set(i,true);
          }
          else
            found.set(i,false);
          j++;
        }
        if(!found.get(i).booleanValue())
        {
          sessionEJB.removeSubject(subs.get(i));  
          System.out.println("Removing " + subs.get(i).getTitle());
        }
      }
      
    }
    catch (Exception e)
    {
      retur = false;
      e.printStackTrace();
    }
    return retur;    
  }
  
  public boolean saveSubjects(domain.Subject s)
    {
      boolean retur = true;
      try
      {
        Subject subject = new Subject();
        subject.setTitle(s.getTitle());
        subject.setDescription(s.getBeskrivelse());
        long id = sessionEJB.querySubjectFindAll().size()+1L;
        subject.setId(id);
        sessionEJB.persistEntity(subject);
      }
      catch (Exception e)
      {
        e.printStackTrace();
        retur = false;
      }
      return retur;
    }
  
  public List<domain.Student> getStudents()
  {
    List<Student> students = sessionEJB.queryStudentFindAll();
    List<Priority> priorities = sessionEJB.queryPriorityFindAll();
    List<domain.Student> studentsS = new ArrayList<domain.Student>();
    
    for (int i = 0; i < students.size(); i++)
    {
      domain.Student st = new domain.Student();
      st.setId(students.get(i).getId());
      st.setName(students.get(i).getName());
      int countFirst = 0;
      int countSecond = 0;
      domain.Subject[] first = new domain.Subject[2];
      domain.Subject[] second = new domain.Subject[2];
      for (int j = 0; j < priorities.size(); j++) 
      {
        if(students.get(i).getId().compareTo(priorities.get(j).getStudent().getId())==0)
        {
          if(priorities.get(j).getPriority()==1)
          {
            domain.Subject s = new domain.Subject();
            s.setId(priorities.get(j).getSubject().getId());
            s.setTitle(priorities.get(j).getSubject().getTitle());
            first[countFirst] = s;
            countFirst++;
          }
          st.setFirst(first);
          if(priorities.get(j).getPriority()==2)
          {
            domain.Subject s = new domain.Subject();
            s.setId(priorities.get(j).getSubject().getId());
            second[countSecond] = s;
            countSecond++;
          }
          st.setSecond(second);          
        }
      } 
      List<Studentsubject> ss = students.get(i).getStudentsubjectList();
      ArrayList<domain.Subject> subs = new ArrayList<domain.Subject>();
      for (int j = 0; j < ss.size(); j++)
      {
        domain.Subject sub = new domain.Subject();
        sub.setId(ss.get(j).getSubject().getId());
        sub.setTitle(ss.get(j).getSubject().getTitle());
        subs.add(sub);
      }
      st.setFinalSubject(subs);

      studentsS.add(st);
    }
    return studentsS;
  }
  
  public List<domain.Pulje> getPuljer()
  {
    List<Pulje> puljer = new ArrayList<Pulje>();
    List<domain.Pulje> puljer2 = new ArrayList<domain.Pulje>();
    puljer = sessionEJB.queryPuljeFindAll();    
    for (int i = 0; i < puljer.size(); i++)
    {
      domain.Pulje p = new domain.Pulje();
      p.setPuljeId(puljer.get(i).getPuljeid());
      domain.Subject s = new domain.Subject();
      
      Subject s1 = puljer.get(i).getSubject();

      System.out.println("Pulje: " + p.getPuljeId() + "Subject  " + s1.getId() );
      s.setId(s1.getId());
      s.setTitle(s1.getTitle());
      p.setSubject(s);
      puljer2.add(p);
    }
    return puljer2;
  }

  public boolean saveFinalSubjects(List<domain.Student> l1)
  {
    boolean retur = true;
    try
    { 
      List<Studentsubject> lists = sessionEJB.queryStudentsubjectFindAll();
      
      for (int i = 0; i < lists.size(); i++)
      {
        System.out.println("Removing ");
        sessionEJB.removeStudentsubject(lists.get(i));
      }


      for (int i = 0; i < l1.size(); i++)
      {
        
        Studentsubject p = new Studentsubject();
        List<domain.Subject> subjects = l1.get(i).getFinals();
        
        for (int j = 0; j < subjects.size(); j++)
        {
          {
            Long sId = subjects.get(j).getId();
            p.setPuljeid(1L);
            Subject s = sessionEJB.querySubjectFindByID(sId.intValue());
            p.setSubject(s);
            Long stId = l1.get(i).getId();
            Student st = sessionEJB.queryStudentFindByID(stId.intValue());
            p.setStudent(st);  
            sessionEJB.persistEntity(p);
          }
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      retur = false;
    }
    return retur;
  }
  
  public boolean savePuljer(List<domain.Subject> l1,List<domain.Subject> l2)
  {
    boolean retur = true;
    try
    { 
      List<Pulje> puljer = sessionEJB.queryPuljeFindAll();
      List<domain.Subject> history1 = new ArrayList<domain.Subject>();
      List<domain.Subject> history2 = new ArrayList<domain.Subject>();
      for (int i = 0; i < puljer.size(); i++)
      {
        System.out.println("Removing: "+ puljer.get(i).getPuljeid() +  " "+ puljer.get(i).getSubjectid());
        sessionEJB.removePulje(puljer.get(i));
      }
      for (int i = 0; i < l1.size(); i++)
      {
        if(!history1.contains(l1.get(i)))
        {
          Pulje p = new Pulje();
          p.setPuljeid(1L);
          Long id = l1.get(i).getId();
          Subject s = sessionEJB.querySubjectFindByID(id.intValue());
          //s.setId(id);
          //s.setTitle(l1.get(i).getTitle());
          //s.setDescription(l1.get(i).getBeskrivelse());
          System.out.println("id1 " + s.getId() + " " + s.getTitle() + " " + s.getDescription());
          p.setSubject(s);
          history1.add(l1.get(i));
          sessionEJB.persistEntity(p);
        }
      }
      for (int i = 0; i < l2.size(); i++)
      {
        if(!history2.contains(l2.get(i)))
        {
          Pulje p2 = new Pulje();
          p2.setPuljeid(2L);
          Long id = l2.get(i).getId();
          Subject s = sessionEJB.querySubjectFindByID(id.intValue());
          //s.setId(l2.get(i).getId());
          //s.setTitle(l2.get(i).getTitle());
          //s.setDescription(l2.get(i).getBeskrivelse());
          System.out.println("id2 " + s.getId() + " " + s.getTitle() + " " + s.getDescription());
          p2.setSubject(s);
          history2.add(l2.get(i));
          sessionEJB.persistEntity(p2);
        }
      }
    }
    catch (Exception e)
    {
      //e.printStackTrace();
      retur = false;
    }
    return retur;
  }
  
  public boolean savePriorities(domain.Student st)
  {
    boolean retur = true;
    
    List<Priority> prio = sessionEJB.queryPriorityFindAll();
    for (int i = 0; i < prio.size(); i++)
    {
      long id = prio.get(i).getStudentid();
      if(st.getId()==id)
      {
        sessionEJB.removePriority(prio.get(i));
      }
    }
    {
      List<domain.Subject> subjects1 = new ArrayList<domain.Subject>();
      List<domain.Subject> subjects2 = new ArrayList<domain.Subject>();
      subjects1.add(st.getFirst()[0]);
      subjects1.add(st.getFirst()[1]);
      subjects2.add(st.getSecond()[0]);
      subjects2.add(st.getSecond()[1]);
      for (int j = 0; j < subjects1.size(); j++) 
      {
        Student s = new Student();
        s.setName(st.getName());
        s.setId(st.getId());
        Priority p = new Priority();
        p.setPriority(1L);
        p.setStudent(s);
        Long subId = subjects1.get(j).getId();
        p.setSubject(sessionEJB.querySubjectFindByID(subId.intValue()));
        sessionEJB.persistEntity(p);
      }
      for (int j = 0; j < subjects2.size(); j++) 
      {
        Student s = new Student();
        s.setName(st.getName());
        s.setId(st.getId());
        Priority p = new Priority();
        p.setPriority(2L);
        p.setStudent(s);
        Long subId = subjects2.get(j).getId();
        p.setSubject(sessionEJB.querySubjectFindByID(subId.intValue()));
        sessionEJB.persistEntity(p);
      }
    }
    return retur;
  }
  
  public static void main(String [] args)
  {
    SessionEJBClient sE = new SessionEJBClient();
    /*List<domain.Student> students = sE.getStudents();
    for (int i = 0; i < students.size(); i++)
    {
      System.out.println(students.get(i).getName()+ " " + students.get(i).getFirst()[0].getTitle());
    }*/
    //sE.saveSubjects();
    //sE.getPuljer();
    
    domain.Student s = new domain.Student();
    s.setId(1L);
    sE.savePriorities(s);
    //sE.saveSubjects();
  }

  private static Context getInitialContext()
    throws NamingException
  {
    Hashtable env = new Hashtable();
    // WebLogic Server 10.x connection details
    env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
    env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
    return new InitialContext( env );
  }
}
